﻿Imports System
Imports Microsoft.Win32
Imports System.Reflection
Imports System.Text.RegularExpressions
Imports System.Configuration
Imports System.IO
Imports System.Text
Imports System.Data
Imports System.Data.SqlClient
Imports System.Xml
Imports System.Environment
Imports System.Net
Imports Microsoft.ServiceHosting.ServiceRuntime
Imports Microsoft.Samples.ServiceHosting.StorageClient
Module Module1
  Public sVersion As String = ""
  Public bParametersLoaded As Boolean = False
  Public iIsInUpgrade As Integer = 0
  Public bFirstTime As Boolean = True

  ' Azure Storage - queue
  Public oQueueEndpointUri As Uri
  Public sAccountName As String = ""
  Public sAccountSharedKey As String = ""

  Public oStorageAccountInfo As StorageAccountInfo
  Public oQueueStorage As QueueStorage
  Public oMessageQueue As MessageQueue

  Public Const cCONFIGQUEUESTORAGEENDPOINT As String = "QueueStorageEndpoint"
  Public Const cCONFIGACCOUNTNAME As String = "AccountName"
  Public Const cCONFIGACCOUNTSHAREDKEY As String = "AccountSharedKey"
  Public Const cQUEUENAME As String = "emmanueltestsystemevents"

  Public oRoleStatus As RoleStatus = RoleStatus.Starting

  Public Sub CheckParameters()
    If Not (bParametersLoaded) Then
      bParametersLoaded = LoadParameters()
      If bParametersLoaded Then
        If bFirstTime Then
          Call LogMessage("Emmanuel WorkerRole started - parameters properly loaded v" + sVersion, EventLogEntryType.Information)
          bFirstTime = False
        End If
      End If
    End If
  End Sub
  Public Function LoadParameters() As Boolean
    Dim bReturn As Boolean = True
    'Dim i As Decimal = 0D
    'Dim j As Decimal = 0D
    'Dim k As Decimal = 0D

    Try
      sVersion = Assembly.GetExecutingAssembly().GetName().Version().ToString
      iIsInUpgrade = CInt(RoleManager.GetConfigurationSetting("IsInUpgrade"))
      'i = j / k

      ' Setup the queue
      oQueueEndpointUri = New Uri(RoleManager.GetConfigurationSetting(cCONFIGQUEUESTORAGEENDPOINT))
      sAccountName = RoleManager.GetConfigurationSetting(cCONFIGACCOUNTNAME)
      sAccountSharedKey = RoleManager.GetConfigurationSetting(cCONFIGACCOUNTSHAREDKEY)

      oStorageAccountInfo = New StorageAccountInfo(oQueueEndpointUri, Nothing, sAccountName, sAccountSharedKey)
      oQueueStorage = QueueStorage.Create(oStorageAccountInfo)
      oMessageQueue = oQueueStorage.GetQueue(cQUEUENAME)

      If Not (oMessageQueue.DoesQueueExist()) Then oMessageQueue.CreateQueue()

      oRoleStatus = RoleStatus.Healthy
      bReturn = True

    Catch ex As Exception
      Call LogMessage("Exception in LoadParameters(): " + ex.ToString, EventLogEntryType.Error)
      oRoleStatus = RoleStatus.Unhealthy
      bReturn = False
    End Try

    Return bReturn
  End Function

  Public Sub LogMessage(ByVal sMessage As String, ByVal oEventLogEntryType As EventLogEntryType)
    Dim sLogInfo As String = ""
    Dim sComputerName As String = ""

    sComputerName = My.Computer.Name

    sLogInfo = "Emmanuel Test Worker Role v" + sVersion + " on " + sComputerName + vbCrLf + vbCrLf
    sLogInfo += sMessage
    sLogInfo += vbCrLf + vbCrLf

    Try
      RoleManager.WriteToLog("Information", sLogInfo)
    Catch ex As Exception
    End Try
  End Sub
End Module
